from requests_html import HTMLSession, AsyncHTMLSession
from echart.mongo_db_help import inser_zone_range,inser_news_range
import requests

from datetime import datetime

# ================百度热点榜单=====================
HOT_RANGE_URL = {
    'real_time': 'http://top.baidu.com/buzz?b=1&fr=topindex',
    'today': 'http://top.baidu.com/buzz?b=341&c=513&fr=topbuzz_b342_c513',
    'seven_day': 'http://top.baidu.com/buzz?b=42&c=513&fr=topbuzz_b341_c513',
    'people_care': "http://top.baidu.com/buzz?b=342&c=513&fr=topbuzz_b42_c513",
    'entertainment': "http://top.baidu.com/buzz?b=344&c=513&fr=topbuzz_b342_c513",
    'sport': "http://top.baidu.com/buzz?b=11&c=513&fr=topbuzz_b344_c513",
}

#============ 百度热点地区比较========================
ZONE_HOT_RANGE_URL = 'http://top.baidu.com/region/singlelist'
# 地区代码-暂时记录了一小部分，用到哪个城市再去查
# key: divids
ZONE_CODE = {
    "913": "广东",
    "912": "广西",
    "916": "江苏",
    "917": "浙江",
    "138": "杭州",
    "95": "广州",
    "94": "深圳",
    "196": "佛山",
    "911": "北京",
    "910": "上海",
}
# 主题代码-暂时记录了一小部分
# key: boardid
THEME_CODE = {
    "26":{"name":"电影","theme":"娱乐"},
    "4":{"name":"电视剧","theme":"娱乐"},
    "19":{"name":"综艺","theme":"娱乐"},
    "23":{"name":"动漫","theme":"娱乐"},
    "258":{"name":"热点人物","theme":"人物"},
    "255":{"name":"体坛人物","theme":"人物"},
    "261":{"name":"财经人物","theme":"人物"},
    "257":{"name":"互联网人物","theme":"人物"},
    "12":{"name":"高校","theme":"生活"},
    "291":{"name":"美食","theme":"生活"},
    "450":{"name":"畅销书","theme":"生活"},
    "2":{"name":"热门","theme":"热门"},
    "396":{"name":"新词","theme":"热门"},
}


def get_hot_news(session, url, type, created, topN):
    r = session.get(url)
    news_list = r.html.find('.list-table', first=True)

    results = []
    trs = news_list.find('tr')

    for tr in trs:
        item = {}

        num = tr.find('.num-top', first=True)
        if num:
            item['range'] = num.text
        else:
            num = tr.find('.num-normal', first=True)
            if num:
                item['range'] = num.text

        try:
            item['keyword'] = tr.find('.keyword',first=True).find('a', first=True).text
        except:
            pass

        if item:
            item['type'] = type
            item['created'] = created
            results.append(item)

        if len(results) >= topN:
            break


    return results


def baidu_hot_range(topN=10):
    session = HTMLSession()
    result = []
    created = datetime.today()
    for key,url in HOT_RANGE_URL.items():
       result += get_hot_news(session,url,key, created, topN=topN)

    inser_news_range(result)

    return result


def get_key(dict, value, name=None):

    if name:
        #如果dict里面还是dict,两级
        return [k for k, v in dict.items() if v[name] == value]

    return [k for k, v in dict.items() if v == value]


def baidu_province(zone_code, theme_code, topN=10):
    url = ZONE_HOT_RANGE_URL
    # boradid　是不同主题
    r = requests.post(url, data={
        'boardid': theme_code,
        'divids[]': zone_code})
    data = r.json()
    # 是否有数据
    items = data['topWords'][zone_code]
    if len(items) == 0:
        return { 'msg':'主题key ID输入错误', 'is_error':True}

    res = {
        'key': theme_code,
        'theme': THEME_CODE[theme_code]['theme'],
        'name': THEME_CODE[theme_code]['name'],
        'ranges': items[:topN],
        'zone': ZONE_CODE[zone_code],
        'zone_id': zone_code,
        'created': datetime.today(),
    }

    inser_zone_range(res)

    return {'is_error': False}


if __name__ == '__main__':
    baidu_hot_range()
    baidu_province('913','26')